1 /**
2 Copyright: Copyright (c) 2021, Joakim Brännström. All rights reserved.
3 License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
4 Author: Joakim Brännström (joakim.brannstrom@gmx.com)
5 
6 Convert a compile_commands.json to an array. Convenient code that is re-used by all engine types.
7 */
8 module code_checker.engine.compile_db;
9 
10 import code_checker.engine.types : Environment;
11 import compile_db : ParsedCompileCommandRange;
12 
13 ParsedCompileCommandRange toRange(Environment env) @safe {
14     import std.algorithm : filter, map;
15     import std.array : array;
16     import my.path;
17     import my.set;
18     import compile_db : parseFlag, CompileCommandFilter, limitOrAllRange, parse, prependFlags,
19         addCompiler, replaceCompiler, addSystemIncludes, fileRange, CompileCommand;
20     import compile_db.user_filerange : ParsedCompileCommandRange;
21 
22     // the following are not needed for now:
23     //.addCompiler
24     //.replaceCompiler
25     //.prependFlags
26     // because they are covered by the unification of the database.
27 
28     auto userFiles = toSet(env.files.map!(a => AbsolutePath(a)));
29 
30     bool userFileFilter(CompileCommand a) {
31         if (userFiles.empty)
32             return true;
33         return a.absoluteFile in userFiles;
34     }
35 
36     Set!AbsolutePath analyzed;
37     auto uniqueFilter = () {
38         if (env.conf.compileDb.dedupFiles) {
39             return (CompileCommand a) {
40                 if (a.absoluteFile in analyzed)
41                     return false;
42                 analyzed.add(a.absoluteFile);
43                 return true;
44             };
45         }
46         return (CompileCommand a) => true;
47     }();
48 
49     auto files = env.compileDb
50         .fileRange
51         .filter!userFileFilter
52         .filter!uniqueFilter
53         .array;
54 
55     // dfmt off
56     return ParsedCompileCommandRange.make(files
57         .parse(env.conf.compileDb.flagFilter)
58         .addSystemIncludes.prependFlags(env.conf.compiler.extraFlags)
59         .array);
60     // dfmt on
61 }